/*******************************************************************************
*  file    : main.cpp
*  created : 09.08.2013
*  author  : Slyshyk Oleksiy (alexSlyshyk@gmail.com)
*******************************************************************************/

#include <QCoreApplication>
#include <QSharedPointer>
#include <QTimer>
#include <QDebug>

#include <gsm.hpp>

#include "maintestobject.hpp"

class PrintMessages : public QObject
{
    Q_OBJECT
public:
    PrintMessages(GsmSmsReader* reader = nullptr) : reader_(nullptr){reader_ = reader;}
public slots:
    void onTime()
    {
        if(reader_)
            {
                for(int i = 0; i < reader_->count(); ++i)
                    {
                        qDebug()<<"id :"<<reader_->at(i)->identifier;
                        qDebug()<<"txt:"<<reader_->at(i)->message.text();
                    }
            }
    }

private:
    GsmSmsReader* reader_;
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QSharedPointer<MainTestObject> main_test = QSharedPointer<MainTestObject>(new MainTestObject);
    qDebug()<<"modem test started";

    QString get = QString("SELECT * FROM incoming "
                          "WHERE text LIKE '%%1%' "
                          "ORDER BY id DESC LIMIT 1;").arg("TEST");
    qDebug()<<"Test query : "<<get;

    GsmModem* m = new GsmModem;
    //m->openSerial("/dev/ttyACM1");
    //m->openSerial("COM6");
    m->init("/dev/ttyUSB0",9600);

    GsmSmsReader* sms_reader = new GsmSmsReader(m);
    sms_reader->setMessageStore("ME");
    sms_reader->check();
    PrintMessages* pm = new PrintMessages(sms_reader);
    QTimer::singleShot(10000, pm, SLOT(onTime()) );

//    GsmSmsSender* sms_sender = new GsmSmsSender(m);
//    QSMSMessage sms;
//    sms.setText("Hello from Qt gsm !");
//    sms.setRecipient("+380688497983");
    //sms_sender->send("1234567890", sms);

    QTimer::singleShot(15000, qApp, SLOT(quit()));

    const int r = a.exec();

    qDebug()<<"modem test ended";
    return r;
    Q_UNUSED(main_test);
}

#include "main.moc"
